﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    using System.Security.Permissions;

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                webBrowser1.Url = new Uri(@"C:\Documents and Settings\ADemko\My Documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\test.html");
                button1.Text = "call script code from client code";
                //   button1.Dock = DockStyle.Top;
                webBrowser1.AllowWebBrowserDrop = false;
                webBrowser1.IsWebBrowserContextMenuEnabled = false;
                webBrowser1.WebBrowserShortcutsEnabled = false;
                webBrowser1.ObjectForScripting = this;
                // Uncomment the following line when you are finished debugging.
                //webBrowser1.ScriptErrorsSuppressed = true;

                webBrowser1.DocumentText =
                    "<html><head><script>" +
                    "function test(message) { alert(message); }" +
                    "</script></head><body><button " +
                    "onclick=\"alert(window.external.Test('called from script code'))\">" +
                    "call client code from script code</button>" +
                    "</body></html>";
            }

            private void button1_Click(object sender, EventArgs e)
            {
                //MessageBox.Show(webBrowser1.Document.InvokeScript("GetSecret").ToString());
                //MessageBox.Show(webBrowser1.Document.GetElementById("div1").InnerHtml);
                webBrowser1.Document.InvokeScript("test",
               new String[] { "called from client code" });
            }

             public string Test(String message)
        {
            MessageBox.Show(message, "client code!!!");
            return "123";
        }

        }
    }




    //using System.Security.Permissions;

    //[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    //[System.Runtime.InteropServices.ComVisibleAttribute(true)]
    //public partial class Form1 : Form
    //{
    //    private void Form1_Load(object sender, EventArgs e)
    //    {
    //        MessageBox.Show("!");
    //        button1.Text = "call script code from client code";
    //     //   button1.Dock = DockStyle.Top;
    //        webBrowser1.AllowWebBrowserDrop = false;
    //        webBrowser1.IsWebBrowserContextMenuEnabled = false;
    //        webBrowser1.WebBrowserShortcutsEnabled = false;
    //        webBrowser1.ObjectForScripting = this;
    //        // Uncomment the following line when you are finished debugging.
    //        //webBrowser1.ScriptErrorsSuppressed = true;

    //        webBrowser1.DocumentText =
    //            "<html><head><script>" +
    //            "function test(message) { alert(message); }" +
    //            "</script></head><body><button " +
    //            "onclick=\"window.external.Test('called from script code')\">" +
    //            "call client code from script code</button>" +
    //            "</body></html>";
    //    }

    //    public void Test(String message)
    //    {
    //        MessageBox.Show(message, "client code");
    //    }

    //    private void button1_Click(object sender, EventArgs e)
    //    {
    //        webBrowser1.Document.InvokeScript("test",
    //            new String[] { "called from client code" });
    //    }

    //}
//}